#######################################################
#A COMMON-SPACE TECHNIQUE FOR VOTERS AND LEGISLATORS
#WITH APPLICATION TO THE 2010/2011 Elections
####################################################### 

##NB - Load the Master file first
library(doParallel)



##We extract Senators 1&2 and identify unique senators
sen1 <- paste(CurrentSen1Name,"-",inputstate)
sen2 <- paste(CurrentSen2Name,"-",inputstate)

sen1_unique <- unique(sen1)
sen2_unique <- unique(sen2)


##create a new cces variable for senate scaling
cces$sen1_scale <- NA
cces$sen2_scale <- NA


##put all senators in a common variable
sen1tmp <- replace(as.numeric(CC334H), as.numeric(CC334H)>7 | is.na(as.numeric(CC334H)), 999)
sen2tmp <- replace(as.numeric(CC334I), as.numeric(CC334I)>7 | is.na(as.numeric(CC334I)), 999)
senc1 <- replace(as.numeric(CC334J), as.numeric(CC334J)>7 | is.na(as.numeric(CC334J)), 999)
senc2 <- replace(as.numeric(CC334K), as.numeric(CC334K)>7 | is.na(as.numeric(CC334K)), 999)

V513 <- replace(CurrentSen1Name, CurrentSen1Name=="", "bbbbbbbbbb")
V521 <- replace(CurrentSen2Name, CurrentSen2Name=="", "cccccccccc")
SenCand1Name[is.na(SenCand1Name)] <- "bbbbbbbbbb"
SenCand2Name[is.na(SenCand2Name)] <- "cccccccccc"
sen1tmp <- (V513==SenCand1Name)*senc1 + (V513==SenCand2Name)*senc2 + (V513!=SenCand1Name & V513!=SenCand2Name)*sen1tmp 
sen2tmp <- (V521==SenCand1Name)*senc1 + (V521==SenCand2Name)*senc2 + (V521!=SenCand1Name & V521!=SenCand2Name)*sen2tmp 

cces$senator1 <- sen1tmp
cces$senator2 <- sen2tmp

################################################
#To the matrix of senators, we add individuals,
#parties, and the president
################################################

self <- CC334A
obama <- CC334C
democrats <- CC334E
republicans <- CC334F

placement_matrix <- cbind(self, obama, democrats, republicans)

for (j in 1:ncol(placement_matrix)){
  placement_matrix[,j] <- as.numeric(placement_matrix[,j])
  placement_matrix[,j] <- replace(placement_matrix[,j], placement_matrix[,j]==8 | is.na(placement_matrix[,j]), 999)
}  

###################################################
#Next, we estimate AM step-by-step for each state
###################################################

##We need one overall estimate across all respondents
overall_scale <- aldmck(placement_matrix[,1:4], polarity=2, respondent=1, missing=999, verbose=FALSE)

#save the overall dem and rep pos
dem_overall <- overall_scale$stim[2]
rep_overall <- overall_scale$stim[3]

states_unique <- unique(inputstate)


##here are the voters' preferences
ideal_points <- overall_scale$resp$ideal

##now senators
placement_holder <- NULL
med_voter <- NULL

for (i in 1:length(states_unique)){
  
  if (states_unique[i]!="District of Columbia"){
    
    #subset by state
    tmp <- subset(cces, inputstate==states_unique[i])  
    temp <- cbind(placement_matrix[inputstate==states_unique[i],], tmp$senator1, tmp$senator2)
    colnames(temp)[5:6] <- c(tmp$CurrentSen1Name[1],tmp$CurrentSen2Name[2])
    
    #remove stimuli that are not measured
    drop <- NULL
    for (j in 1:ncol(temp)) if(sum(temp[,j]==999, na.rm=TRUE)==nrow(temp)) drop <- c(drop, j)
    if (sum(drop)>0) temp <- temp[,-drop]
    
    scaling <- aldmck(temp, polarity=2, respondent=1, missing=999, verbose=FALSE)
    
    registerDoParallel(cores=8)
    scaling_boot <- foreach(i=1:200, .combine='cbind') %dopar% 
      aldmck(temp[sample(1:nrow(temp),nrow(temp),replace=TRUE),], polarity=2, respondent=1, 
             missing=999)$stim
    
    #recale from state space back to overall space
    #using the equ OVERALL = slope*STATE - slope*STATE_DEM + OVERALL_DEM
    
    slope <- (overall_scale$stim[3]-overall_scale$stim[2])/(scaling$stim[3]-scaling$stim[2])
    rescaling <- slope*scaling$stim - slope*scaling$stim[2] + overall_scale$stim[2]
    
    rescaling_boot <- slope*scaling_boot[4:nrow(scaling_boot),] - slope*scaling$stim[2] + overall_scale$stim[2]
    
    #add results to cces dataset...
    cces$sen1_scale[inputstate==states_unique[i]] <- rescaling[4]
    cces$sen2_scale[inputstate==states_unique[i]] <- rescaling[5]
    
    #save results for elites
    if (sum(dim(rescaling_boot))==0){
      tmp <- c(rescaling[4], quantile(rescaling_boot,c(0.05,0.95)))
      tmp <- data.frame(names(rescaling)[4],t(tmp))
      colnames(tmp) <- c("senator","idealpt","lower","upper")
      placement_holder <- rbind(placement_holder, tmp)
    }else{
      tmp <- c(rescaling[4], quantile(rescaling_boot[1,],c(0.05,0.95)))
      tmp <- data.frame(names(rescaling)[4],t(tmp))
      colnames(tmp) <- c("senator","idealpt","lower","upper")
      placement_holder <- rbind(placement_holder, tmp)
      tmp <- c(rescaling[5], quantile(rescaling_boot[2,],c(0.05,0.95)))
      tmp <- data.frame(names(rescaling)[5],t(tmp))
      colnames(tmp) <- c("senator","idealpt","lower","upper")
      placement_holder <- rbind(placement_holder, tmp)
    }
    
    #and for voters
    voter_boot <- foreach(i=1:200, .combine='cbind') %dopar% 
      aldmck(temp[sample(1:nrow(temp),nrow(temp),replace=TRUE),], polarity=2, respondent=1, 
             missing=999)$resp$idealpt
    m_boot <- apply(voter_boot,2,mean,na.rm=TRUE)
    m_boot <- slope*m_boot - slope*scaling$stim[2] + overall_scale$stim[2]
    
    tmp2 <- c(mean(m_boot), quantile(m_boot,c(0.05,0.95)))
    tmp2 <- data.frame(states_unique[i],t(tmp2))
    colnames(tmp2) <- c("state","mean_voter","m_lower","m_upper")
    med_voter <- rbind(med_voter, tmp2)
    
    print(placement_holder)
  }
}


library(ggplot2)

placement_holder$senator <- factor(placement_holder$senator)
placement_holder$senator <- reorder(placement_holder$senator, placement_holder$idealpt)

ggplot(placement_holder, aes(x = idealpt, xmin = lower, xmax = upper, y = senator )) +
  geom_point() + geom_segment( aes(x = lower, xend = upper,
                                   y = senator, yend=senator)) +
  theme_bw() + theme(axis.title.x = element_text(size = 12, vjust = .25), legend.position="none") +
  xlab("Ideal Points") + ylab("") 



